"""Notification models for the trading platform."""

from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Text, Enum, JSON, Index
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base


class Notification(Base):
    """Notification model representing user notifications."""

    __tablename__ = 'notifications'

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    title = Column(String(255), nullable=False)
    content = Column(Text)
    notification_type = Column(Enum('system', 'trade', 'message', 'evaluation', 'product'), nullable=False)
    related_id = Column(Integer)
    is_read = Column(Boolean, default=False)
    data = Column(JSON)
    created_at = Column(DateTime, default=func.now())

    # Relationships
    user = relationship("User")

    # Indexes
    __table_args__ = (
        Index('idx_notification_user_id', 'user_id'),
        Index('idx_is_read', 'is_read'),
    )


class RefreshToken(Base):
    """Refresh token model for user authentication."""

    __tablename__ = 'refresh_tokens'

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    token = Column(String(512), unique=True, nullable=False)
    expires_at = Column(DateTime, nullable=False)
    is_revoked = Column(Boolean, default=False)
    created_at = Column(DateTime, default=func.now())

    # Relationships
    user = relationship("User")


class UserOnlineStatus(Base):
    """User online status model representing user online status and connection info."""

    __tablename__ = 'user_online_status'

    user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
    is_online = Column(Boolean, default=False)
    last_seen_at = Column(DateTime)
    socket_id = Column(String(255))
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

    # Relationships
    user = relationship("User")